package com.yj.service.impl;

import com.yj.entity.Comments;
import com.yj.entity.Users;
import com.yj.dao.CommentsMapper;
import com.yj.dao.UsersMapper;
import com.yj.service.ICommentsService;
import com.yj.util.PageUtil;
import com.yj.vo.JsonResult;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *评论表
 * @author yj
 * @since 2020-08-24
 */
@Service
public class CommentsServiceImpl extends ServiceImpl<CommentsMapper, Comments> implements ICommentsService {

	@Autowired
	CommentsMapper commentsmapper;
	
	@Autowired
	UsersMapper usermappper;
	
	@Override
	public JsonResult showComments(int hid) {
		JsonResult retJsonResult=null;
		QueryWrapper< Comments> wrapper=new QueryWrapper<>();
		wrapper.in("hid", hid).orderByDesc("pid");
		ArrayList retList=new ArrayList<>();
		List<Comments> listcomment=commentsmapper.selectList(wrapper);
		if(listcomment.size()>0) {
			for(Comments comm:listcomment) {
				HashMap map=new HashMap();
				Users users=usermappper.selectById(comm.getUid());
				map.put("uname",users.getUname());
				map.put("comment", comm);
				map.put("img", users.getUimg());
				retList.add(map);
			}
			retJsonResult=new JsonResult(true, "评论信息查询成功");
			retJsonResult.setData("listcomments", retList);
			return retJsonResult;
		}else {
			retJsonResult =new JsonResult(false, "没有查到评论信息");
			return retJsonResult;
		}
		
		
	}

	@Override
	public JsonResult delComments(int pid,HttpSession session) {
		JsonResult ret=null;
		Comments comments=commentsmapper.selectById(pid);	
		Object value = session.getAttribute("usersession");
		if(value!=null) {
			Users users=(Users)value;
			if(users.getUid()==comments.getUid()) {
				int num=commentsmapper.deleteById(pid);
				if(num>0) {
					ret =new JsonResult(true, "评论删除成功");
					return ret;
				}else {
					ret=new JsonResult(false, "评论删除失败");
					return ret;
				}
			}else {
				ret=new JsonResult(false, "此评论您无法删除");
				return ret;
			}
			
			
		}else {
			
			ret=new JsonResult(false, "未登录无法删除");
			return ret;
			
		}
		
		
		
		
	}

	@Override
	public JsonResult addComments(Comments comments,HttpSession session) {
		JsonResult jsonResult=new JsonResult(false, null);
		Object value = session.getAttribute("usersession");
		if(value!=null) {
			Users users=(Users)value;
			comments.setUid(users.getUid());
			int num=commentsmapper.insert(comments);
			if(num>0) {
				jsonResult.setMessage("添加评论成功");
				jsonResult.setSuccess(true);
			}else {
				jsonResult.setMessage("添加评论失败");
				
			}
			
		}else {
			jsonResult.setMessage("您还未登录，请先登录");
		}
		
		return jsonResult;
	}
	
	
	
	@Override
	public JsonResult pages() {
		
		return PageUtil.pages(commentsmapper, null, 6);
	}

	@Override
	public JsonResult page(int pageNum) {
		
		return PageUtil.page(pageNum, 6, commentsmapper, null);
	}

	@Override
	public JsonResult delComments(int pid) {
		JsonResult jsonResult=new JsonResult(false, null);
		int num=commentsmapper.deleteById(pid);
		if (num>0) {
			jsonResult.setMessage("删除评论成功");
		}else {
			jsonResult.setMessage("删除评论失败");
		}
		return jsonResult;
	}

}
